package com.bitnpulse.dev2.jjh.engstudybook_free.page;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.bitnpulse.dev2.jjh.engstudybook_free.R;
import com.bitnpulse.dev2.jjh.engstudybook_free.db.WritingDB;
import com.bitnpulse.dev2.jjh.engstudybook_free.dialog.TwoButtonDialog;
import com.bitnpulse.dev2.jjh.engstudybook_free.listener.IPopUpButtonEventListener;

public class DiaryPage extends Activity{

	public String[] cData = new String[5];
	
	EditText diary_text01, diary_text02;
	
	String Contents_ID;
	String Today;
	
	private TwoButtonDialog mTwoButtonDialog_;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.diarypage_layout);
		
		Intent intent = getIntent();
		Contents_ID = intent.getStringExtra("Contents_ID");
		Today = intent.getStringExtra("TODAY");
		
		diary_text01 = (EditText)findViewById(R.id.diary_text01);
		diary_text02 = (EditText)findViewById(R.id.diary_text02);
		
		diary_text01.setPrivateImeOptions("defaultInputmode=english;");
		diary_text02.setPrivateImeOptions("defaultInputmode=english;");
		
		diary_text01.setHint(Today);
		
		mTwoButtonDialog_ = new TwoButtonDialog(this);
		
		mTwoButtonDialog_.setButtonListener(new IPopUpButtonEventListener() {
			
			@Override
			public void buttonEvent(int id) {
				switch (id) {
					case 1:
						finish();
						break;
					case 2:
						saveDiary() ;
						finish();
						break;
				}
			}
		});
		
		db_Init();
	}
	
    protected void db_Init(){
    	
    	db_read();
    }
    
    
    
    public void db_read(){
//		userTable_sqldata = new WritingDB(this);
//    	userTable_db = userTable_sqldata.getReadableDatabase();
    	WritingDB mwriting_Db = new WritingDB(this);
    	
		Cursor c = mwriting_Db.getReadableDatabase().query(WritingDB.TABLE_NAME, WritingDB.FROM, WritingDB.CONTENT_ID + " LIKE " +"'%" + Contents_ID + "%'", null, null, null, null);
		
		
		 while (c.moveToNext()) { 
				cData[0] = c.getString(0);
				cData[1] = c.getString(1);
				cData[2] = c.getString(2);
				cData[3] = c.getString(3);
				cData[4] = c.getString(4);
				
//				System.out.println("JJJ DiaryPage.db_read()" + cData[0] + "++");
		 }
				
		 if(cData[0] != null && !cData[0].isEmpty()){
			 diary_text01.setText(cData[2]);
			 diary_text02.setText(cData[3]);
		 }
		 

		c.close();
		mwriting_Db.close();
		
	}
    
	public void btnClick(View view) {
		
		saveDiary() ;
		
		finish();
	}
    
	
	public void saveDiary() {
		WritingDB mwriting_Db = new WritingDB(this);
		
		ContentValues values = new ContentValues();
		
		
		if(cData[0] == null || cData[0].isEmpty()){
			
			values.put(WritingDB.CONTENT_ID, Contents_ID);
			
			values.put(WritingDB.DIARY_1, diary_text01.getText().toString());
			values.put(WritingDB.DIARY_2, diary_text02.getText().toString());
			
			values.put(WritingDB.COMPOSITION, "");
//				
			long table_id = mwriting_Db.getReadableDatabase().insertOrThrow(WritingDB.TABLE_NAME, null, values);
		
		
		}else{
			
			values.put(WritingDB.DIARY_1, diary_text01.getText().toString());
			values.put(WritingDB.DIARY_2, diary_text02.getText().toString());
			
			String sFilter = WritingDB._ID + " LIKE '%" + cData[0] + "%'";
			
			mwriting_Db.getReadableDatabase().update(WritingDB.TABLE_NAME, values, sFilter , null);
		}
    	mwriting_Db.close();
		
	}
	
	@Override
	public void onBackPressed() {
		
		if(cData[2] != null && cData[2].equals(diary_text01.getText().toString()) && cData[3] != null && cData[3].equals(diary_text02.getText().toString()) ){
			super.onBackPressed();
		}else if (cData[2] == null && cData[3] == null && diary_text01.getText().toString().isEmpty() && diary_text02.getText().toString().isEmpty()  ){
			super.onBackPressed();
		}else{
			mTwoButtonDialog_.show();
			mTwoButtonDialog_.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		}
		
	}
}
